package com.dq.controller.api.merchant.workbench.staff;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.CustomerQuery;
import com.dq.controller.in.request.StaffUserAdd;
import com.dq.vo.Vo;
import com.dq.domain.base.AbstractEntity;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author liuhui
 * @date 2019/11/26 0026 14:35:49
 */
@RestController
@RequestMapping("/api/merchant/workbench/staff/archives")
@Api(description = "员工档案")
public class StaArchivesController extends AllService {


    @PostMapping("/list")
    @ApiOperation("名称或手机号检索员工档案列表")
    public Vo list(@RequestBody CustomerQuery customerQuery)throws AdminException{
        /*if(null==customerQuery.getPage() || null==customerQuery.getSize()){throw new AdminException("缺少必要的请求参数");}
        List<StaffUser> staffUserList = staffUserService.findByWhere(customerQuery);
        Long totalCount = staffUserService.countByWhere(customerQuery);
        List<StaffUser.StaffUserFileListVo> staffUserFileListVos = new ArrayList<>();
        staffUserList.forEach(staffUser -> {staffUserFileListVos.add(staffUser.toStaffUserFileListVo());});
        PageInfo pageInfo = new PageInfo(staffUserFileListVos);
        pageInfo.setTotal(totalCount);
        return new Vo(pageInfo);*/
        return null;
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("员工档案详情查询")
    public Vo detail(@PathVariable Long id)throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,id);
        if(null==staffUser){throw new AdminException("员工信息不存在");}
        Long averageScore =evaluationRecordRepository.findByScore(staffUser.getId());
        Long num = evaluationRecordRepository.findByNum(staffUser.getId());
        BigDecimal average;
        if(num == 0L){
            average = new BigDecimal("0");
        }else {
            average = new BigDecimal(averageScore.toString()).divide(new BigDecimal(num.toString()),1, RoundingMode.HALF_UP);
        }
        staffUser.setAverage(average);
        return new Vo(staffUser.toStaffUserDetailVo());
    }

    @PostMapping("/edit/{userId}/{positionId}")
    @ApiOperation("修改员工档案")
    public Vo edit(@PathVariable Long userId,@PathVariable Long positionId,@RequestBody StaffUser staffUserEdit)throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,userId);
        if(null==staffUser){throw new AdminException("员工信息不存在");}
        OrgPosition orgPosition = dao.get(OrgPosition.class,positionId);
        if(null==orgPosition){throw new AdminException("岗位信息不存在");}
        staffUser.setOrgPosition(orgPosition);

        BeanUtils.copyProperties(staffUserEdit,staffUser, BeanNullUtil.getNullPropertyNames(staffUserEdit));

        // 员工档案 判空
        staffUserService.judgeObject(staffUser, staffUser.getId());

        staffUserRepository.save(staffUser);
        return new Vo(HttpStatus.OK);
    }

    @PostMapping("/add/loginname")
    @ApiOperation("员工账号添加")
    public HashMap<String, Object> addAccount(@RequestBody StaffUserAdd sysLoginUser)throws AdminException{
        if(StringUtils.isEmpty(sysLoginUser.getUsername()) || StringUtils.isEmpty(sysLoginUser.getPassword())){
            throw new AdminException("账号密码不可为空");
        }
        SysUser sysUser = shiroService.getUser();
        StaffUser staffUser = new StaffUser();
        if(null==staffUser){throw new AdminException("员工信息不存在");}
        staffUser.setCardNo(AbstractEntity.generyDatetimeAndRandomNum(Constant.ORDERS_SUFF_COUNT));
        staffUser.setUsername(sysLoginUser.getUsername());
        staffUser.setPassword(sysUserService.getPassword(sysLoginUser.getPassword()));
        staffUser.setSection(sysUser.getSection());
        staffUser.setHeadImg(sysLoginUser.getImgUrl());
        staffUser.setUserRealcName(sysLoginUser.getUserRealName());
        staffUser.setStatus(SysUser.Status.on);
        StaffUser save = staffUserRepository.save(staffUser);
        HashMap<String,Object> map = new HashMap();
        map.put("code","200");
        map.put("message",HttpStatus.OK);
        map.put("data",save.toStaffUserDetailVo());
        return map;
    }
    @PostMapping("/add/{positionId}")
    @ApiOperation("员工档案添加")
    public Vo add(@PathVariable Long positionId, @RequestBody StaffUser staffUserAdd)throws AdminException{
        OrgPosition orgPosition = dao.get(OrgPosition.class,positionId);
        if(null==orgPosition){throw new AdminException("岗位信息不存在");}
        staffUserAdd.setOrgPosition(orgPosition);

        staffUserService.save(staffUserAdd);

        return new Vo(HttpStatus.OK);
    }

    @PostMapping("/edit/loginname/{userId}")
    @ApiOperation("修改员工账号")
    public Vo edit(@PathVariable Long userId,@RequestBody StaffUserAdd staffUserAdd)throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,userId);
        if(null==staffUser){throw new AdminException("员工信息不存在");}

        BeanUtils.copyProperties(staffUserAdd,staffUser, BeanNullUtil.getNullPropertyNames(staffUserAdd));

        if(StringUtils.isNotEmpty(staffUserAdd.getPassword())){
            staffUser.setPassword(sysUserService.getPassword(staffUserAdd.getPassword()));
        }

        // 员工账号 判空
        staffUserService.judgeAcObject(staffUser, staffUser.getId());

        staffUserRepository.save(staffUser);
        return new Vo(HttpStatus.OK);
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除档案")
    public Vo delete(@PathVariable Long id)throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,id);
        if(staffUser == null){
            throw new AdminException("请重新选择");
        }
        staffUser.setDeleted(true);
        staffUserRepository.save(staffUser);
        return new Vo(HttpStatus.OK);
    }

    @PostMapping("staffQuit/{id}")
    @ApiOperation("员工离职")
    public Vo staffQuit(@PathVariable Long id)throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,id);
        if(staffUser == null){
            throw new AdminException("请重新选择");
        }
        staffUserRepository.save(staffUser);
        return new Vo(HttpStatus.OK);
    }

}
